from future import division

pyvisa

Probably won't use this library because it doesn't work on ubuntu out of the box


In [ ]:
import visa
rm = visa.ResourceManager()

pyusb


In [7]:
import usb.core

In [8]:
# usb.core.show_devices(verbose=True)
usb.core.show_devices()


Out[8]:
DEVICE ID 8087:8000 on Bus 004 Address 002, Hub
DEVICE ID 1d6b:0002 on Bus 004 Address 001, Hub
DEVICE ID 8087:8008 on Bus 003 Address 002, Hub
DEVICE ID 1d6b:0002 on Bus 003 Address 001, Hub
DEVICE ID 1d6b:0003 on Bus 002 Address 001, Hub
DEVICE ID 13d3:3394 on Bus 001 Address 003, Wireless Controller
DEVICE ID 1ab1:0588 on Bus 001 Address 005, Specified at interface
DEVICE ID 1d6b:0002 on Bus 001 Address 001, Hub

In [9]:
for dev in usb.core.find(find_all=True):
#     print(dev)
#     print(dir(dev))
#     print(dev.idVendor)
#     print(dev.idProduct)
    if dev.idVendor == 6833:
        print(dev)


DEVICE ID 1ab1:0588 on Bus 001 Address 005 =================
 bLength                :   0x12 (18 bytes)
 bDescriptorType        :    0x1 Device
 bcdUSB                 :  0x110 USB 1.1
 bDeviceClass           :    0x0 Specified at interface
 bDeviceSubClass        :    0x0
 bDeviceProtocol        :    0x0
 bMaxPacketSize0        :   0x40 (64 bytes)
 idVendor               : 0x1ab1
 idProduct              : 0x0588
 bcdDevice              :  0x100 Device 1.0
 iManufacturer          :    0x1 Rigol Technologies
 iProduct               :    0x2 DS1000 SERIES 
 iSerialNumber          :    0x3 DS1EB142004074
 bNumConfigurations     :    0x1
  CONFIGURATION 1: 2 mA ====================================
   bLength              :    0x9 (9 bytes)
   bDescriptorType      :    0x2 Configuration
   wTotalLength         :   0x27 (39 bytes)
   bNumInterfaces       :    0x1
   bConfigurationValue  :    0x1
   iConfiguration       :    0x0 
   bmAttributes         :   0xc0 Self Powered
   bMaxPower            :    0x1 (2 mA)
    INTERFACE 0: Application Specific ======================
     bLength            :    0x9 (9 bytes)
     bDescriptorType    :    0x4 Interface
     bInterfaceNumber   :    0x0
     bAlternateSetting  :    0x0
     bNumEndpoints      :    0x3
     bInterfaceClass    :   0xfe Application Specific
     bInterfaceSubClass :    0x3
     bInterfaceProtocol :    0x1
     iInterface         :    0x0 
      ENDPOINT 0x1: Bulk OUT ===============================
       bLength          :    0x7 (7 bytes)
       bDescriptorType  :    0x5 Endpoint
       bEndpointAddress :    0x1 OUT
       bmAttributes     :    0x2 Bulk
       wMaxPacketSize   :   0x40 (64 bytes)
       bInterval        :    0x0
      ENDPOINT 0x82: Bulk IN ===============================
       bLength          :    0x7 (7 bytes)
       bDescriptorType  :    0x5 Endpoint
       bEndpointAddress :   0x82 IN
       bmAttributes     :    0x2 Bulk
       wMaxPacketSize   :   0x40 (64 bytes)
       bInterval        :    0x0
      ENDPOINT 0x83: Interrupt IN ==========================
       bLength          :    0x7 (7 bytes)
       bDescriptorType  :    0x5 Endpoint
       bEndpointAddress :   0x83 IN
       bmAttributes     :    0x3 Interrupt
       wMaxPacketSize   :    0x8 (8 bytes)
       bInterval        :    0x1

In [12]:
dev = usb.core.find(idVendor=0x1ab1, idProduct=0x0588)
if dev.is_kernel_driver_active(0):
    reattach = True
    dev.detach_kernel_driver(0)
dev.set_configuration()


---------------------------------------------------------------------------
USBError                                  Traceback (most recent call last)
<ipython-input-12-51d27d47a741> in <module>()
      3     reattach = True
      4     dev.detach_kernel_driver(0)
----> 5 dev.set_configuration()

/home/brian/.local/lib/python2.7/site-packages/usb/core.pyc in set_configuration(self, configuration)
    797         without arguments is enough to get the device ready.
    798         """
--> 799         self._ctx.managed_set_configuration(self, configuration)
    800 
    801     def get_active_configuration(self):

/home/brian/.local/lib/python2.7/site-packages/usb/core.pyc in managed_set_configuration(self, device, config)
    126 
    127         self.managed_open()
--> 128         self.backend.set_configuration(self.handle, cfg.bConfigurationValue)
    129 
    130         # cache the index instead of the object to avoid cyclic references

/home/brian/.local/lib/python2.7/site-packages/usb/backend/libusb1.pyc in set_configuration(self, dev_handle, config_value)
    728     @methodtrace(_logger)
    729     def set_configuration(self, dev_handle, config_value):
--> 730         _check(self.lib.libusb_set_configuration(dev_handle.handle, config_value))
    731 
    732     @methodtrace(_logger)

/home/brian/.local/lib/python2.7/site-packages/usb/backend/libusb1.pyc in _check(ret)
    550             raise NotImplementedError(_strerror(ret))
    551         else:
--> 552             raise USBError(_strerror(ret), ret, _libusb_errno[ret])
    553 
    554     return ret

USBError: [Errno 16] Resource busy

In [11]:
cfg = dev.get_active_configuration()
intf = cfg[(0,0)]

In [33]:
cfg[(0,0)]


Out[33]:
<INTERFACE 0: Application Specific>

In [13]:
# print(dir(intf))
intf.endpoints()


Out[13]:
(<ENDPOINT 0x1: Bulk OUT>,
 <ENDPOINT 0x82: Bulk IN>,
 <ENDPOINT 0x83: Interrupt IN>)

In [35]:
ep = usb.util.find_descriptor(intf, 
    custom_match = lambda e: usb.util.endpoint_direction(e.bEndpointAddress) == usb.util.ENDPOINT_OUT)
print(ep)


      ENDPOINT 0x1: Bulk OUT ===============================
       bLength          :    0x7 (7 bytes)
       bDescriptorType  :    0x5 Endpoint
       bEndpointAddress :    0x1 OUT
       bmAttributes     :    0x2 Bulk
       wMaxPacketSize   :   0x40 (64 bytes)
       bInterval        :    0x0

In [45]:
dev.write(0x82, "*IDN?", timeout=10000)


---------------------------------------------------------------------------
USBError                                  Traceback (most recent call last)
<ipython-input-45-1a8ee41751bd> in <module>()
----> 1 dev.write(0x82, "*IDN?", timeout=10000)

/home/brian/.local/lib/python2.7/site-packages/usb/core.pyc in write(self, endpoint, data, timeout)
    876                 intf.bInterfaceNumber,
    877                 _interop.as_array(data),
--> 878                 self.__get_timeout(timeout)
    879             )
    880 

/home/brian/.local/lib/python2.7/site-packages/usb/backend/libusb1.pyc in bulk_write(self, dev_handle, ep, intf, data, timeout)
    758                             intf,
    759                             data,
--> 760                             timeout)
    761 
    762     @methodtrace(_logger)

/home/brian/.local/lib/python2.7/site-packages/usb/backend/libusb1.pyc in __write(self, fn, dev_handle, ep, intf, data, timeout)
    854         # do not assume LIBUSB_ERROR_TIMEOUT means no I/O.
    855         if not (transferred.value and retval == LIBUSB_ERROR_TIMEOUT):
--> 856             _check(retval)
    857 
    858         return transferred.value

/home/brian/.local/lib/python2.7/site-packages/usb/backend/libusb1.pyc in _check(ret)
    550             raise NotImplementedError(_strerror(ret))
    551         else:
--> 552             raise USBError(_strerror(ret), ret, _libusb_errno[ret])
    553 
    554     return ret

USBError: [Errno 110] Operation timed out

failure

usbtmc


In [1]:
import usbtmc

In [7]:
devices = usbtmc.list_devices()

In [8]:
for dev in devices:
    print(dev.manufacturer)
    print(dev.idProduct)
    print(dev.idVendor)


---------------------------------------------------------------------------
USBError                                  Traceback (most recent call last)
<ipython-input-8-3993aaddfa01> in <module>()
      1 for dev in devices:
----> 2     print(dev.manufacturer)
      3     print(dev.idProduct)
      4     print(dev.idVendor)

/home/brian/.local/lib/python2.7/site-packages/usb/core.pyc in manufacturer(self)
    785         """
    786         if self._manufacturer is None:
--> 787             self._manufacturer = util.get_string(self, self.iManufacturer)
    788         return self._manufacturer
    789 

/home/brian/.local/lib/python2.7/site-packages/usb/util.pyc in get_string(dev, index, langid)
    259                     254,
    260                     DESC_TYPE_STRING,
--> 261                     0
    262                 )
    263         assert len(buf) >= 4

/home/brian/.local/lib/python2.7/site-packages/usb/control.pyc in get_descriptor(dev, desc_size, desc_type, desc_index, wIndex)
    171             wValue = wValue,
    172             wIndex = wIndex,
--> 173             data_or_wLength = desc_size)
    174 
    175 def set_descriptor(dev, desc, desc_type, desc_index, wIndex = None):

/home/brian/.local/lib/python2.7/site-packages/usb/core.pyc in ctrl_transfer(self, bmRequestType, bRequest, wValue, wIndex, data_or_wLength, timeout)
    953             buff = _interop.as_array(data_or_wLength)
    954 
--> 955         self._ctx.managed_open()
    956 
    957         # Thanks to Johannes Stezenbach to point me out that we need to

/home/brian/.local/lib/python2.7/site-packages/usb/core.pyc in managed_open(self)
    103     def managed_open(self):
    104         if self.handle is None:
--> 105             self.handle = self.backend.open_device(self.dev)
    106         return self.handle
    107 

/home/brian/.local/lib/python2.7/site-packages/usb/backend/libusb1.pyc in open_device(self, dev)
    720     @methodtrace(_logger)
    721     def open_device(self, dev):
--> 722         return _DeviceHandle(dev)
    723 
    724     @methodtrace(_logger)

/home/brian/.local/lib/python2.7/site-packages/usb/backend/libusb1.pyc in __init__(self, dev)
    598         self.handle = _libusb_device_handle()
    599         self.devid = dev.devid
--> 600         _check(_lib.libusb_open(self.devid, byref(self.handle)))
    601 
    602 class _IsoTransferHandler(object):

/home/brian/.local/lib/python2.7/site-packages/usb/backend/libusb1.pyc in _check(ret)
    550             raise NotImplementedError(_strerror(ret))
    551         else:
--> 552             raise USBError(_strerror(ret), ret, _libusb_errno[ret])
    553 
    554     return ret

USBError: [Errno 13] Access denied (insufficient permissions)

In [18]:
d = usbtmc.list_devices()[0]
print(dir(d))
print(d.manufacturer)


['_Device__default_timeout', '_Device__get_def_tmo', '_Device__get_timeout', '_Device__set_def_tmo', '__class__', '__del__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__getitem__', '__hash__', '__init__', '__iter__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_ctx', '_get_full_descriptor_str', '_manufacturer', '_product', '_serial_number', '_str', 'address', 'attach_kernel_driver', 'bDescriptorType', 'bDeviceClass', 'bDeviceProtocol', 'bDeviceSubClass', 'bLength', 'bMaxPacketSize0', 'bNumConfigurations', 'bcdDevice', 'bcdUSB', 'bus', 'clear_halt', 'configurations', 'ctrl_transfer', 'default_timeout', 'detach_kernel_driver', 'get_active_configuration', 'iManufacturer', 'iProduct', 'iSerialNumber', 'idProduct', 'idVendor', 'is_kernel_driver_active', 'manufacturer', 'port_number', 'product', 'read', 'reset', 'serial_number', 'set_configuration', 'set_interface_altsetting', 'write']
Rigol Technologies

In [3]:
instr = usbtmc.Instrument(0x1ab1, 0x0588)

In [4]:
instr.device


Out[4]:
<DEVICE ID 1ab1:0588 on Bus 001 Address 005>

In [5]:
instr.ask("*IDN?")


Out[5]:
u'Rigol Technologies,DS1102E,DS1EB142004074,00.03.01.00.00'
Finally a success